import { createRouter, createWebHashHistory } from 'vue-router';
import { userStore } from '../store/user';
import pina from '../store/store.js'

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Layout/index.vue'),
    children: [
      {
        path: '',
        redirect: '/home/userManagement',
      },
      {
        path: 'userManagement',
        name: 'userManagement',
        component: () => import('../views/user-manngment/index.vue'),
        children: [
          {
            path: 'detail',
            name: 'userManagement',
            component: () => import('../views/user-manngment/detail.vue'),
          },
        ],
      },
      {
        path: 'categoryManagment',
        name: 'categoryManagment',
        component: () => import('../views/category-managment/index.vue'),
      },
      {
        path: 'foodManagment',
        name: 'foodManagment',
        component: () => import('../views/food-managment/index.vue'),
        children: [
          {
            path: 'detail',
            name: 'foodManagment',
            component: () => import('../views/food-managment/detail.vue'),
          },
        ],
      },
      {
        path: 'packageManagment',
        name: 'packageManagment',
        component: () => import('../views/package-managment/index.vue'),
        children: [
          {
            path: 'detail',
            name: 'packageManagment',
            component: () => import('../views/package-managment/detail.vue'),
          },
        ],
      },
      {
        path: 'orderDeatil',
        name: 'orderDeatil',
        component: () => import('../views/order-detail/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue'),
  },
];
const router = createRouter({
  routes,
  history: createWebHashHistory(),
});
/**
 * 全局路由拦截，判断的当前是否已经登录
 */
router.beforeEach((to, from, next) => {
  const user = userStore(pina);
  if (to.name !== 'Login') {
    if (user.userData || window.localStorage.getItem('userData')) {
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
});
export default router;
